import 'package:flutter/material.dart';
import 'package:play_android/com/zhihaoliang/const/AppEventBus.dart';
import 'package:play_android/com/zhihaoliang/http/HttpRequest.dart';
import 'package:play_android/com/zhihaoliang/play/login/event/LoginEvent.dart';
import 'package:play_android/com/zhihaoliang/play/login/view/LoginPage.dart';
import 'package:play_android/com/zhihaoliang/util/SpUtil.dart';
import 'package:play_android/com/zhihaoliang/const/Appkey.dart';

import 'AboutUsPage.dart';
import 'CollectPage.dart';

///  ***************************************************************************
///  右面的"我的"的页面
///  ***************************************************************************

class MinePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MinePageState();
  }
}

class _MinePageState extends State<MinePage> with WidgetsBindingObserver {
  String userName;

  @override
  void initState() {
    super.initState();

    HttpRequest().isCookie().then((isLogin) {
      SpUtil().getString(AppKey.ACCOUNT).then((name) {
        setState(() {
          userName = name;
        });
      });
    });

    AppEventBus.eventBus.on<LoginEvent>().listen((event) {
      setState(() {
        userName = event.name;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    Widget image = Image.asset(
      'images/avatar.jpeg',
      width: 100.0,
      height: 100.0,
    );

    Widget raisedButton = RaisedButton(
      child: Text(
        userName == null ? "请登录" : userName,
        style: TextStyle(color: Colors.white),
      ),
      color: Theme.of(context).accentColor,
      onPressed: () {
        //登录
        HttpRequest().isCookie().then((isLogin) {
          if (!isLogin) {
            Navigator.of(context)
                .push(MaterialPageRoute(builder: ((context) => LoginPage())));
          }
        });
      },
    );

    Widget listLike = ListTile(
        leading: const Icon(Icons.favorite),
        title: const Text('喜欢的文章'),
        trailing:
            Icon(Icons.chevron_right, color: Theme.of(context).accentColor),
        onTap: () {
          HttpRequest().isCookie().then((isLogin) {
            if (isLogin) {
              Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                return CollectPage();
              }));
            }else{
              Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                return LoginPage();
              }));
            }

          });
        });

    Widget listAbout = ListTile(
        leading: const Icon(Icons.info),
        title: const Text('关于我们'),
        trailing:
            Icon(Icons.chevron_right, color: Theme.of(context).accentColor),
        onTap: () {
          Navigator.of(context).push(MaterialPageRoute(builder: (context) {
            return AboutUsPage();
          }));
        });

    Widget listLogout = ListTile(
        leading: const Icon(Icons.info),
        title: const Text('退出登录'),
        trailing:
            Icon(Icons.chevron_right, color: Theme.of(context).accentColor),
        onTap: () {
          HttpRequest().clearCookie().then((_) {
            setState(() {
              userName = null;
            });
          });
        });

    return ListView(
      padding: const EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
      children: <Widget>[
        image,
        raisedButton,
        listLike,
        listAbout,
        listLogout,
      ],
    );
  }
}
